/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.leon.ui.tree;

import java.io.File;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Justin
 */
public class FileNode {

    public final ImageIcon ICON_FOLDER = new ImageIcon(getClass().getResource("/icon/folder.jpg"));
    public final ImageIcon ICON_EXPANDEDFOLDER = new ImageIcon(getClass().getResource("/icon/expandfolder.jpg"));
    public final ImageIcon ICON_FILE = new ImageIcon(getClass().getResource("/icon/file.jpg"));
    public final ImageIcon ICON_SOURCE_FILE = new ImageIcon(getClass().getResource("/icon/source_file.jpg"));
    protected File m_file;

    public FileNode(File file) {
        m_file = file;
    }

    public File getFile() {
        return m_file;
    }

    @Override
    public String toString() {
        return m_file.getName().length() > 0 ? m_file.getName() : m_file.getPath();
    }

    public boolean expand(DefaultMutableTreeNode parent) {
        DefaultMutableTreeNode flag = (DefaultMutableTreeNode) parent.getFirstChild();
        if (flag == null) // No flag
        {
            return false;
        }
        Object obj = flag.getUserObject();
        if (!(obj instanceof Boolean)) {
            return false;      // Already expanded
        }
        parent.removeAllChildren();  // Remove Flag

        File[] files = listFiles();
        if (files == null) {
            return true;
        }

        Vector v = new Vector();

        for (int k = 0; k < files.length; k++) {
            File f = files[k];
            if (/*!(f.isDirectory() || f.getName().endsWith(".java") || f.getName().endsWith(".h") || f.getName().endsWith(".cpp") || f.getName().endsWith(".c"))||*/f.isHidden()) {
                continue;
            }

            FileNode newNode = new FileNode(f);

            boolean isAdded = false;
            for (int i = 0; i < v.size(); i++) {
                FileNode nd = (FileNode) v.elementAt(i);
                if (newNode.compareTo(nd) < 0) {
                    v.insertElementAt(newNode, i);
                    isAdded = true;
                    break;
                }
            }
            if (!isAdded) {
                v.addElement(newNode);
            }
        }

        for (int i = 0; i < v.size(); i++) {
            IconData idata;
            FileNode nd = (FileNode) v.elementAt(i);
            String name = nd.m_file.getName().toLowerCase();
            if (nd.m_file.isDirectory()) {
                idata = new IconData(ICON_FOLDER, ICON_EXPANDEDFOLDER, nd);
            }
            else if(name.endsWith(".java")||name.endsWith(".c")||name.endsWith(".cpp")||name.endsWith(".h")){
                idata = new IconData(ICON_SOURCE_FILE , nd);
            }
            else {
                idata = new IconData(ICON_FILE, nd);
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(idata);
            parent.add(node);

            if (nd.hasSubDirs()) {
                node.add(new DefaultMutableTreeNode(new Boolean(true)));
            }
        }

        return true;
    }

    public boolean hasSubDirs() {
        File[] files = listFiles();
        if (files == null) {
            return false;
        }
        for (int k = 0; k < files.length; k++) {
            if (files[k] != null) {
                return true;
            }
        }
        return false;
    }

    public int compareTo(FileNode toCompare) {
        return m_file.getName().compareToIgnoreCase(toCompare.m_file.getName());
    }

    protected File[] listFiles() {
        if (!m_file.isDirectory()) {
            return null;
        }
        try {
            return m_file.listFiles();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error reading directory " + m_file.getAbsolutePath(), "Warning", JOptionPane.WARNING_MESSAGE);
            return null;
        }
    }
}
